package co.chonghao.ch_kuaichong_user.utils;

import android.util.Log;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 检测账号、密码等信息格式的工具类
 */
public class InputValidator {

    private static final String TAG = "InputValidator";

    public static final String ERROR1 = "请输入正确手机号";
    public static final String ERROR2 = "密码长度为6到20位";
    public static final String ERROR3 = "密码不能包含空格";
    public static final String ERROR4 = "请输入正确验证码";
    public static final String ERROR5 = "邮箱格式不正确";
    public static final String ERROR6 = "密码格式不正确，需同时包含数字和字母";

    public static final String BINGO = "BINGO";

    public static String checkMobileNumAndPwd(String mobileNum, String password) {
        String result = checkMobileNum(mobileNum);
        if (BINGO.equals(result)) {
            result = checkPassword(password);
            if (BINGO.equals(result)) {
                return BINGO;
            }
        }
        return result;
    }

    public static String checkMobileNumAndPwdAndCode(String mobileNum, String password, String code) {
        String result = checkMobileNum(mobileNum);
        if (BINGO.equals(result)) {
            result = checkPassword(password);
            if (BINGO.equals(result)) {
                result = checkCodeNumber(code);
                if (BINGO.equals(result)) {
                    return BINGO;
                }
            }
        }
        return result;
    }

    /**
     * 检查手机号
     *
     * @param mobileNum 手机号字符串
     * @return 输出校验结果
     */
    public static String checkMobileNum(String mobileNum) {
        Pattern p = Pattern.compile("^(13|14|15|17|18)\\d{9}$");
        Matcher m = p.matcher(mobileNum);
        if (m.matches()) {
            return BINGO;
        } else
            return ERROR1;
    }

    /**
     * 检查密码字符串
     *
     * @param password 密码字符串
     * @return 输出校验结果
     */
    public static String checkPassword(String password) {
        if (password.length() < 6 || password.length() > 20) {
            return ERROR2;
        } else if (password.contains(" ")) {
            return ERROR3;
        }
//        else if (!hasNumAndLetter(password))
//            return ERROR6;
        return BINGO;
    }

    /**
     * @param code 输入验证码
     * @return 输出校验结果
     */
    public static String checkCodeNumber(String code) {
        String str = "^\\d{6}$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(code);
        if (m.matches()) {
            return BINGO;
        } else
            return ERROR4;
    }

    /**
     * @param address 输入验证码
     * @return 输出校验结果
     */
    public static String checkEmailAddress(String address) {

        return BINGO;
    }

    //判断字符串是否同时包含数字和字母
    private static boolean hasNumAndLetter(String str) {
        boolean isLetter = false;
        boolean isNum = false;
        Pattern pattern = Pattern.compile("^[0-9a-zA-Z]+$");//包含数字和字母，没有其他字符
        for (int i = 0; i < str.length() && (!isLetter); i++) {
            if (Character.isLetter(str.charAt(i)))
                isLetter = true;
        }
        for (int i = 0; i < str.length() && (!isNum); i++) {
            if (Character.isDigit(str.charAt(i)))
                isNum = true;
        }
        Log.i(TAG, "包含数字: " + isNum);
        Log.i(TAG, "包含字母: " + isLetter);
        Log.i(TAG, "包含数字: " + pattern.matcher(str).matches());
        return isLetter && isNum && pattern.matcher(str).matches();

    }
    /**
     * 检查身份证
     * @param cardNum 身份证字符串
     * @return 输出校验结果
     */
    public static boolean checkCardNum(String cardNum) {
        Pattern p = Pattern.compile("^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$|^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X|x)$");
        Matcher m = p.matcher(cardNum);
        if (m.matches()) {
            return true;
        } else
            return false;

    }

}
